package com.example.play.config.cors;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;


/**
 * @author wsl
 * @version 1.0
 * @date 2020/5/18
 * @description 这是服务器跨域处理
 */

@Configuration
public class CustomCorsConfiguration {


    /***
     * 在spring MVC 中可以配置全局的规则，
     * 也可以使用@CrossOrigin注解进行细粒度的配置。
     * @return
     */

    @Bean
    public FilterRegistrationBean corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource ();
        CorsConfiguration config = new CorsConfiguration ();
        config.setAllowCredentials (true);
        config.addAllowedOrigin ("*");
        config.addAllowedHeader ("*");
        config.addAllowedMethod ("OPTION");
        config.addAllowedMethod ("GET");
        config.addAllowedMethod ("POST");
        config.addAllowedMethod ("PUT");
        config.addAllowedMethod ("HEAD");
        config.addAllowedMethod ("DELETE");
        source.registerCorsConfiguration ("/**", config);
        FilterRegistrationBean bean = new FilterRegistrationBean (new CorsFilter (source));
        bean.setOrder (0);
        return bean;
    }

    @Bean
    public WebMvcConfigurer mvcConfigurer() {
        return new WebMvcConfigurerAdapter () {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping ("/**").allowedMethods ("GET", "PUT", "POST", "GET", "OPTIONS");
            }
        };
    }


}
